package com.zygh.policeasset.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.mapper.EachTypeImagesMapper;
import com.zygh.policeasset.mapper.UserMapper;
import com.zygh.policeasset.pojo.EachTypeImages;
import com.zygh.policeasset.pojo.User;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import sun.misc.BASE64Decoder;

@Service
/* loaded from: police-asset-2.0-0.0.1-SNAPSHOT.jar:BOOT-INF/classes/com/zygh/policeasset/service/UserService.class */
public class UserService {
    @Autowired
    private UserMapper userMapper;
    private String JpgContentType = "image/jpeg";
    @Autowired
    MinioService minioService;
    @Autowired
    EachTypeImagesMapper eachTypeImagesMapper;

    public PageInfo findAllUser(int pageNum, int pageSize, String name, Integer departmentId) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<>(this.userMapper.findAllUser(name, departmentId));
    }

    public List<User> findAllUserNoPage(String name) {
        return this.userMapper.findAllUser(name, null);
    }

    public User findUser(String name, Integer departmentId) {
        return this.userMapper.findAllUser(name, departmentId).get(0);
    }

    public Integer addUser(User user) {
        user.setCreateTime(DateUtil.now());
        return this.userMapper.addUser(user);
    }

    public Integer updateUser(User user) {
        user.setUpdateTime(DateUtil.now());
        return this.userMapper.updateUser(user);
    }

    public Integer deleteUser(int id) {
        return this.userMapper.deleteUser(id);
    }

    public User findByUsername(String username) {
        return this.userMapper.findByUsername(username);
    }

    public User findByUserId(Integer id) {
        return this.userMapper.findByUserId(id);
    }

    public int updateByUserNP(Integer id, String password, Integer firstTimeLogin) {
        return this.userMapper.updateByUserNP(id, password, firstTimeLogin, null, null).intValue();
    }

    public int updateSignatureLastTime(Integer id) {
        return this.userMapper.updateByUserNP(id, null, null, DateUtil.now(), null).intValue();
    }

    public CommonResult selectSignatureTime(Integer id) {
        return new CommonResult().success(this.userMapper.selectSignatureTime(id));
    }
    public CommonResult updateUuidById(Integer id) {
        return new CommonResult().success(this.userMapper.updateUuidById(id));
    }

    public String findById(Integer id) {
        return this.userMapper.findById(id);
    }

    @Transactional(rollbackFor = {Exception.class})
    public CommonResult signature(String base64, Integer userId) throws IOException {
        try {
            String objectName = "signature/" + userId + "/" + IdUtil.objectId() + ".jpg";
            this.minioService.upload(objectName, new ByteArrayInputStream(new BASE64Decoder().decodeBuffer(base64.split("base64,")[1])), this.JpgContentType, "assets");
            this.eachTypeImagesMapper.add(userId, "http://41.6.251.17:9000/assets/" + objectName, DateUtil.now(), "signature");
            return new CommonResult().success();
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new CommonResult().validateFailed(e.toString());
        }
    }

    public CommonResult selectSignature(Integer userId) {
        try {
            List<EachTypeImages> list = this.eachTypeImagesMapper.findbyTypeIdAndType(userId, "signature");
            if (list.size() > 0) {
                return new CommonResult().success(list.get(0));
            }
            return new CommonResult().success();
        } catch (Exception e) {
            return new CommonResult().validateFailed(e.toString());
        }
    }
}